/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年08月07日 09:01:03
 *
 */

package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.commons.CollectionUtils;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Operator;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.BizDeptProxy;
import com.pactera.jep.sys.service.BizDeptProxyService;
import com.pactera.jep.sys.service.RoleStdService;
import com.pactera.jep.web.code.WebCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "代管管理")
@RestController
@RequestMapping("/bizDeptProxy")
public class BizDeptProxyRestController extends BaseRestController<BizDeptProxy> {
    Logger logger = LoggerFactory.getLogger(BizDeptProxyRestController.class);

    @Autowired
    private BizDeptProxyService bizDeptProxyService;

    @Autowired
    private RoleStdService roleStdService;

    @Override
    protected void prepareQuery(String token, Page<BizDeptProxy> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {
        bizDeptProxyService.query(page, filters, orders);
    }

    @ApiOperation(value = "保存代管信息")
    @PostMapping(value = "/save")
    public Map<String, Object> save(@RequestBody BizDeptProxy bizDeptProxy) {
        Map<String, Object> tips = new HashMap<String, Object>();
        Boolean isExist = bizDeptProxyService.exists(bizDeptProxy.getId());
        try {
            if (isExist) {
                bizDeptProxyService.update(bizDeptProxy);
            } else {
                bizDeptProxyService.insert(bizDeptProxy);
            }
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
        } catch (ServiceException e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
        }
        return tips;
    }

    @ApiOperation(value = "根据id删除代管信息")
    @DeleteMapping(value = "/remove/{id}")
    public Map<String, Object> remove(@PathVariable String id) {
        Map<String, Object> tips = new HashMap<String, Object>();
        try {
            bizDeptProxyService.deleteByPrimaryKey(id);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
        } catch (ServiceException e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
        }
        return tips;
    }

    @ApiOperation(value = "验证提交的id是否已经存在数据库中")
    @PostMapping(value = "/check", produces = "application/json;charset=UTF-8")
    public Map<String, Object> check(@RequestBody String id) {
        Map<String, Object> tips = new HashMap<String, Object>();
        BizDeptProxy exist = bizDeptProxyService.get(id);
        if (exist == null) {
            tips.put("valid", true);
        } else {
            tips.put("valid", false);
        }
        return tips;
    }

    @ApiOperation(value = "验证提交的被代管部门和标准角色是否已经存在数据库中（相同的部门和相同的角色不能重复）")
    @PostMapping(value = "/checkRepeat", produces = "application/json;charset=UTF-8")
    public Map<String, Object> checkRepeat(@RequestBody String deptCode, String stdRoleCode) {
        Map<String, Object> tips = new HashMap<String, Object>();
        /*查询部门的标准角色是否已经存在*/
        List<Filter> filters = new ArrayList<Filter>();
        filters.add(new Filter("deptCode", Operator.EQ, deptCode));
        filters.add(new Filter("stdRoleCode", Operator.EQ, stdRoleCode));
        List<BizDeptProxy> exist = bizDeptProxyService.select(filters);
        if (CollectionUtils.isEmpty(exist)) {
            tips.put("valid", true);
        } else {
            tips.put("bizDeptProxyID", exist.get(0).getId());
            tips.put("valid", false);
        }
        return tips;
    }

}